package com.bryandismas.pcsx.gui;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.bryandismas.pcsx.R;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class FileChooser extends ListActivity
{
	class FileListTextView extends LinearLayout
	{
		TextView text;
		
		public FileListTextView(Context context)
		{
			super(context);
			
			this.setOrientation(VERTICAL);
			
			text = new TextView(context);
			text.setTextSize(20);
			text.setPadding(10, 10, 10, 10);
			addView(text, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		}
		
		public void setText(String text)
		{
			this.text.setText(text);
		}
	}
	
	class FileListAdapter extends BaseAdapter
	{
		Context context;
		List<String> items;
		
		public FileListAdapter(Context context)
		{
			items = new ArrayList<String>();
			this.context = context;
		}
		
		public void clear()
		{
			items.clear();
		}
		
		public void add(String item)
		{
			items.add(item);
		}

		@Override
		public int getCount()
		{
			return items.size();
		}

		@Override
		public Object getItem(int position)
		{
			return items.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			FileListTextView ret;
			if (convertView == null)
			{
				ret = new FileListTextView(context);
			}
			else
			{
				ret = (FileListTextView)convertView;
			}
			ret.setText(items.get(position));
			return ret;
		}
	}
	
	String currentDirectory;
	FileListAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.file_chooser);
		
		String title = "Pcsx Android - Choose File";
		String defaultdir = "/sdcard";
		Intent parent = getIntent();
		if (parent != null)
		{
			title = parent.getExtras().getString("Title");
			defaultdir = parent.getExtras().getString("DefaultDirectory");
		}
		
		setTitle(title);
		currentDirectory = defaultdir;
		
		refreshFileList();
		getListView().setItemsCanFocus(true);
	}
	
	void refreshFileList()
	{
		adapter = new FileListAdapter(this);
		adapter.clear();
		adapter.add("..");
		
		File dir = new File(currentDirectory);
		String[] files = dir.list();
		Arrays.sort(files);
		for (int i = 0; i < files.length; i ++)
		{
			String file = files[i];
			adapter.add(file);
		}
		setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		String selected = (String)l.getItemAtPosition(position);
		currentDirectory += "/" + selected;
		
		File file = new File(currentDirectory);
		if (file.isDirectory())
		{
			refreshFileList();
		}
		else
		{
			Intent intent = new Intent();
			intent.putExtra("SelectedFile", currentDirectory);
			setResult(1, intent);
			finish();
		}
	}
}
